﻿using HandyControl.Controls;
using OMRON.Compolet.CIPCompolet64;
using Ping9719.DeviceCommunication;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Runtime.Remoting.Contexts;
using System.Text;
using System.Threading.Tasks;

namespace BaseUi
{
    /// <summary>
    /// 欧姆龙
    /// </summary>
    public class OmronBLL
    {
        string ip;
        int post;
        /// <summary>
        /// PLC
        /// </summary>
        public NJCompolet Compolet;

        public OmronBLL(string ip, int port)
        {
            this.ip = ip;
            this.post = port;

            Compolet = new NJCompolet(new Container());
            //Compolet = new NJCompolet();
        }

        /// <summary>
        /// 打开
        /// </summary>
        public void Open()
        {
            Task.Run(() =>
            {
                while (true)
                {
                    System.Threading.Thread.Sleep(1000);
                    if (!Compolet.IsConnected)
                    {
                        try
                        {
                            Compolet.ConnectionType = ConnectionType.Class3;
                            Compolet.PeerAddress = ip;
                            Compolet.LocalPort = post;
                            Compolet.ReceiveTimeLimit = 750;
                            Compolet.PlcEncoding = Encoding.UTF8;//编码属性
                            Compolet.DontFragment = false;//是否启用通信中读写禁止，默认关闭
                            Compolet.Active = true;

                            //return new Result();
                        }
                        catch (Exception ex)
                        {
                            //return new Result() { IsSucceed = false, Err = ex.Message, Exception = ex };
                        }
                    }
                }
            });

        }

        /// <summary>
        /// 读数据
        /// </summary>
        /// <typeparam name="T">类型</typeparam>
        /// <param name="addr">地址</param>
        /// <returns>结果</returns>
        public Result<T> Read<T>(string addr)
        {
            try
            {
                if (Compolet.IsConnected)
                {
                    var aaa = Compolet.ReadVariable(addr);
                    return new Result<T>((T)aaa);
                }
                else
                {
                    return new Result<T>() { IsSucceed = false, Err = "连接失败" };
                }
            }
            catch (Exception ex)
            {
                return new Result<T>() { IsSucceed = false, Err = ex.Message, Exception = ex };
            }
        }

        /// <summary>
        /// 写数据
        /// </summary>
        /// <param name="add">地址</param>
        /// <param name="val">数据</param>
        /// <returns>结果</returns>
        public Result Write(string add, object val)
        {
            try
            {
                if (Compolet.IsConnected)
                {
                    Compolet.WriteVariable(add, val);
                    return new Result();
                }
                else
                {
                    return new Result() { IsSucceed = false, Err = "连接失败" };
                }
            }
            catch (Exception ex)
            {
                return new Result() { IsSucceed = false, Err = ex.Message, Exception = ex };
            }
        }
    }
}
